<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>使用嵌套字段排序 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-sorting.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-sorting.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="nested-objects.html">嵌套对象</a></span>
»
<span class="breadcrumb-node">使用嵌套字段排序</span>
</div>
<div class="navheader">
<span class="prev">
<a href="nested-query.html">« 嵌套对象查询</a>
</span>
<span class="next">
<a href="nested-aggregation.html">嵌套聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="nested-sorting"></a>使用嵌套字段排序<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/402_Nested/33_Nested_sorting.asciidoc">edit</a>
</h2>
</div></div></div>
<p>尽管嵌套字段的值存储于独立的嵌套文档中，但依然有方法按照嵌套字段的值排序。  让我们添加另一个记录，以使得结果更有意思：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /my_index/blogpost/2
{
  "title": "Investment secrets",
  "body":  "What they don't tell you ...",
  "tags":  [ "shares", "equities" ],
  "comments": [
    {
      "name":    "Mary Brown",
      "comment": "Lies, lies, lies",
      "age":     42,
      "stars":   1,
      "date":    "2014-10-18"
    },
    {
      "name":    "John Smith",
      "comment": "You're making it up!",
      "age":     28,
      "stars":   2,
      "date":    "2014-10-16"
    }
  ]
}</pre>
</div>
<p>假如我们想要查询在10月份收到评论的博客文章，并且按照 <code class="word">stars</code> 数的最小值来由小到大排序，那么查询语句如下：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "nested": { <a id="CO273-1"></a><i class="conum" data-value="1"></i>
      "path": "comments",
      "filter": {
        "range": {
          "comments.date": {
            "gte": "2014-10-01",
            "lt":  "2014-11-01"
          }
        }
      }
    }
  },
  "sort": {
    "comments.stars": { <a id="CO273-2"></a><i class="conum" data-value="2"></i>
      "order": "asc",   <a id="CO273-3"></a><i class="conum" data-value="2"></i>
      "mode":  "min",   <a id="CO273-4"></a><i class="conum" data-value="2"></i>
      "nested_path": "comments", <a id="CO273-5"></a><i class="conum" data-value="3"></i>
      "nested_filter": {
        "range": {
          "comments.date": {
            "gte": "2014-10-01",
            "lt":  "2014-11-01"
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO273-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>此处的 <code class="literal">nested</code> 查询将结果限定为在10月份收到过评论的博客文章。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO273-2"><i class="conum" data-value="2"></i></a><a href="#CO273-3"></a><a href="#CO273-4"></a></p>
</td>
<td align="left" valign="top">
<p>结果按照匹配的评论中 <code class="word">comment.stars</code> 字段的最小值 (<code class="literal">min</code>) 来由小到大 (<code class="literal">asc</code>) 排序。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO273-5"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>排序子句中的 <code class="literal">nested_path</code> 和 <code class="literal">nested_filter</code> 和 <code class="literal">query</code> 子句中的 <code class="literal">nested</code> 查询相同，原因在下面有解释。</p>
</td>
</tr>
</table>
</div>
<p>为什么要在<code class="literal">nested_filter</code>中重复查询条件呢?<span style="color:#666">(前面的quer.nested.path/filter已经定义过)</span> 原因是<b>排序发生在查询执行之后</b>。
查询要匹配在10月份收到评论的博客，但返回的是博客文档。
如果不加入<code class="literal">nested_filter</code>子句，那么对文档的排序将基于博客的所有评论，而不是仅仅在10月份接收到的评论。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="nested-query.html">« 嵌套对象查询</a>
</span>
<span class="next">
<a href="nested-aggregation.html">嵌套聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>